# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json


class PuserDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()

    def add_puser_info(self, user_id, user_info):
        cur = self.conn.cursor()

        puser_id = user_id
        puser_name = user_info['puser_name']
        puser_job = user_info['puser_job']
        puser_address = user_info['puser_address']
        puser_idea = user_info['puser_idea']
        puser_code = user_info['puser_code']
        puser_province = user_info['puser_province']
        puser_city = user_info['puser_city']
        puser_town = user_info['puser_town']

        sql = "insert into zzh_puser (puser_id,puser_name,puser_job,puser_address,puser_idea,puser_code," \
              "puser_province,puser_city,puser_town) " \
              "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"

        result = cur.execute(sql, [puser_id, puser_name, puser_job, puser_address, puser_idea,puser_code,
                                   puser_province,puser_city,puser_town])
        self.conn.commit()

        sql2 = "update zzh_user set user_type='1' WHERE user_id=%s"
        cur.execute(sql2, [user_id])
        self.conn.commit()

        cur.close()
        self.conn.close()
        return result

    def update_puser(self, user_id, user_info):
        cur = self.conn.cursor()
        puser_name = user_info['puser_name']
        puser_job = user_info['puser_job']
        puser_address = user_info['puser_address']
        puser_email = user_info['puser_email']
        puser_idea = user_info['puser_idea']
        puser_img = user_info['puser_img']
        puser_code = user_info['puser_code']

        puser_province = user_info['puser_province']
        puser_city = user_info['puser_city']
        puser_town = user_info['puser_town']

        sql = "update zzh_puser set puser_name=%s,puser_job=%s,puser_address=%s,puser_email=%s,puser_idea=%s,puser_img=%s,puser_code=%s," \
              "puser_province=%s,puser_city=%s,puser_town=%s" \
              " WHERE puser_id=%s"
        result = cur.execute(sql, [puser_name, puser_job, puser_address, puser_email, puser_idea, puser_img,puser_code,
                                   puser_province,puser_city,puser_town, user_id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return result

    def query_puser_info(self, user_id):
        cur = self.conn.cursor()
        sql = "select * from zzh_puser where puser_id=%s"
        result = cur.execute(sql, [user_id])
        puser_list = cur.fetchall()
        puser_json = Table2Json.puser_list_to_json(puser_list)
        return puser_json

    def query_puser_by_id(self,user_id):
        cur = self.conn.cursor()
        sql = "select * from zzh_puser where puser_id=%s"
        result = cur.execute(sql, [user_id])
        puser_list = cur.fetchone()

        return puser_list



